/**
 *       Copyright (C) 2006-2016 by DSS Corporation.
 * This document contains proprietary information of DSS Corporation.
 * No distribution is granted except with written permission from
 * DSS Corporation.
 *
 * @version $Revision:$  $Date:$
 */
  

package dsc.model.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import com.dss.dsc.GlobalDefine;
import com.dss.dsc.Layout.ToolMethod;
import com.dss.dsc.model.*;
import com.dss.dsc.model.namespace.GlobalDSNameCache;

import dsc.model.Location;
import dsc.model.Array;
import dsc.model.view.*;
import dsc.model.view.SysForView.Complete.Link;
import dsc.model.view.SysForView.Complete.Link.LinkShowInfo;
import dsc.model.view.SysForView.Complete.Link.LinkType;
import dsc.model.view.SysForView.Complete.Sys.Content;
import dsc.model.view.SysForView.Complete;
import dsc.model.view.SysForView.Complete.Sys;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint.Point;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.EndPoint;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.PreCondition;
import dsc.model.view.SysForView.Complete.Sys.Content.Composite.SubArray;
import dsc.model.view.SysForView.Complete.Sys.Property;
import dsc.model.view.SysForView.Complete.Sys.ShowInfo;
import dsc.model.view.SysForView.Complete.Sys.ShowInfo.Rectangle;
import dsc.model.view.SysForView.Complete.Sys.SpecSys;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SNameLabel;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SNameLabel.NameLabelType;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SProto;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SWork;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SWork.DeriveInfo;
import dsc.model.view.SysForView.Complete.Sys.SpecSys.SWork.DeriveInfo.DeriveFlag;
import dsc.model.view.SysForView.Complete.Sys.Type;
import dsc.model.view.YesNo;

/**
 * @author yinhe
 *
 */
public class GefModelUtil {
	public List<AbstractConnectionModel> connListBuf = new ArrayList<AbstractConnectionModel>();
	private static GefModelUtil inst = null;
	private String namePrefix = "$Dsc_";
	
	public static GefModelUtil instance() {
		if (inst == null) {
			inst = new GefModelUtil();
		}
		return inst;
	}
	
	public Object readFile(String fileName) {
		try {
			File targetFile = new File(fileName);
			if (!targetFile.exists()) {
				return null;
			}

			ObjectInputStream objectInput = new ObjectInputStream(
					new FileInputStream(targetFile));
			Object obj = objectInput.readObject();
			objectInput.close();

			return obj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void convertContentsModel(ContentsModel content, Sys sys, Array<Link> links) {
		connListBuf.clear();
		List<AbstractModel> children = content.getChildren();
		
		for (AbstractModel child : children) {
			if (child instanceof DSLabel) {
				continue;
			}
			if(child.getName().equals("DSS_TaskAbstractIA") 
					|| child.getName().equals("DSS_HumanAbstractIA") ){
				continue;
			}
			
			Sys subSys = new Sys();
			subSys.superSys = new Location();
			subSys.superSys.target = sys;
			convertAbstractModel(child, subSys);
			if(sys.content == null){
				sys.content = new Content();
			}
			if (sys.content.composite().subSys == null) {
				sys.content.composite().subSys = new Array<Sys>();
			}
			sys.content.composite().subSys.inst.add(subSys);
		}
		
		convertConnection(links);
	}
	
//	public void convertExitPoint(){
//		
//	}
	
	public void convertAbstractModel(AbstractModel model, Sys sys) {
		sys.name = new StringBuffer(model.getName());
		if(model.getName().equals("")){
			sys.name.append("$sys"+model.getDiagramId());
		}
	//	System.out.println(model.getName());
		model.sysObj = sys;
		if (model instanceof Data) {
			sys.type = Type.Data;
		} else if (model instanceof ExpressionElement) {
			if (model.getDsType() == GlobalDefine.Type_SimplePredicate) {
				sys.type = Type.Predicate;
			} else {
				sys.type = Type.Work;
			}
		} else if (model instanceof IA) {
			sys.type = Type.Work;
		} else if (model instanceof EA) {
			sys.type = Type.Protocol;
		} else if (model instanceof Predicate) {
			sys.type = Type.Predicate;
		} else if (model instanceof SimpleSystem || model instanceof CompositeSystem) {
			setSystemType(model, sys);
		} else if (model instanceof DS){
			setDSType(model, sys);
		} else if (model instanceof NameLabel) {
			sys.type = Type.NameLabel;
		} else if (model instanceof SequentialBlock) {
			sys.type = Type.SequentialBlock;
		} else if (model instanceof CommentLabel){
			sys.type = Type.Comment;
		}
		
		sys.property = new Property();
		sys.property.instantiate = YesNo.no;
		if (model.hasShadow()) {
			sys.property.hasShadow = YesNo.yes;
			if (model.getInstanceNumber().length() > 0) {
				String num = model.getInstanceNumber();
				if(model.getInstanceNumber().equals("0")){
					num = "";
				}
				sys.property.staticCnt = new StringBuffer(num);
			}
		}
		
		if (model instanceof Node) {
			String userDefinedType = ((Node) model).getUserDSType();
			if (userDefinedType.length() > 0) {
				sys.userDefinedType = new StringBuffer(userDefinedType);
			}
		}
		
		sys.content = new Content();
		if (model instanceof SimpleSystem) {
			setSimpleContent(sys,((SimpleSystem) model).getContent());
			convertSpecSys(model, sys);
		} else if (model instanceof SimpleData) {
			setSimpleContent(sys,((SimpleData) model).getType());
		} else if (model instanceof ExpressionElement) {
			setSimpleContent(sys, ((ExpressionElement) model).getExpression());
			convertSpecSys(model, sys);
		} else if (model instanceof SimpleEA) {
			setSimpleContent(sys,((SimpleEA) model).getEventType());
			convertSpecSys(model, sys);
		} else if (model instanceof SequentialBlock) {
			convertSequentialBlock((SequentialBlock) model, sys);
		} else if (model instanceof IComposite) {
			convertCompositeModel(model, sys);
			convertSpecSys(model, sys);
		} else if (model instanceof NameLabel) {
			//sys.name.append(((NameLabel) model).getName());
			String nameLabel = model.getName();
			sys.name = new StringBuffer("nameLabel"+model.diagramId);
			sys.specSys = new SpecSys();
			sys.specSys.sNameLabel().nameLabel = new StringBuffer(nameLabel);
			if (model.hasShadow() && nameLabel.length() > 0 
					&& nameLabel.charAt(nameLabel.length()-1) != ']') {
				sys.specSys.sNameLabel().nameLabel.append("[*]");
			}
			convertCompositeModel(model, sys);
		} else if(model instanceof CommentLabel){
			String text = ((CommentLabel) model).getText();
			text = text.replaceAll("<", "&lt;");
			text = text.replaceAll(">", "&gt;");
			text = text.replaceAll("\n", "&#13;");;
			setSimpleContent(sys, text);
			System.out.println(text);
		}
		
		//showInfo
		if(model instanceof Node){
			sys.showInfo = new ShowInfo();
			sys.showInfo.rectangle.x = model.getConstraint().x;
			sys.showInfo.rectangle.y = model.getConstraint().y;
			sys.showInfo.rectangle.w = model.getConstraint().width;
			sys.showInfo.rectangle.h = model.getConstraint().height;
			
			
			if(((Node)model).isCollapsed()){
				sys.showInfo.showContent = YesNo.no;
			}else{
				sys.showInfo.showContent = YesNo.yes;
			}
			
			if(((Node)model).getShowNameFlag()){
				sys.showInfo.showLabel = YesNo.yes;
			}else{
				sys.showInfo.showLabel = YesNo.no;
			}
			
			if(((Node)model).isVisible()){
				sys.showInfo.visible = YesNo.yes;
			}else{
				sys.showInfo.visible = YesNo.no;
			}
			
			if(model.getParent() instanceof Node && ((Node)model.getParent()).getShowNameFlag()){
				sys.showInfo.rectangle.y -= GlobalDefine.LABEL_HIGH;
			}
			
			bufConnection(((Node) model).getSourceConnection());
			bufConnection(((Node) model).getTargetConnection());
		}
		
	}
	
	private void setSimpleContent(Sys sys,Object content){
		if(content instanceof String){
			sys.content.simple().expr();
			sys.content.simple().picked = new StringBuffer((String) content);
		}		
		
	}
	
	private void setDSType(AbstractModel model,Sys sys){
		int type = model.getDsType();
		if(type == GlobalDefine.Type_Data){
			sys.type = Type.Data;
		}else if(type == GlobalDefine.Type_Predicate){
			sys.type = Type.Predicate;
		}else if(type == GlobalDefine.Type_EA){
			sys.type = Type.Protocol;
		}else if(type == GlobalDefine.Type_WORK){
			sys.type = Type.Work;
		}else if(type == GlobalDefine.Type_SYSTEM){
			sys.type = Type.System;
		}
	}
	
	private void setSystemType(AbstractModel model, Sys sys) {
		int type = model.getDsType();
		if (type == GlobalDefine.Type_SIMPLE_WORK) {
			sys.type = Type.Work;
		} else if (type == GlobalDefine.Type_SIMPLE_WORK_PROG) {
			sys.type = Type.Work;
		} else if (type == GlobalDefine.Type_COMPOSITE_WORK) {
			sys.type = Type.Work;
		} else if (type == GlobalDefine.Type_COMPOSITE_WORK_PROG) {
			sys.type = Type.Work;
		} else if (type == GlobalDefine.Type_SimplePredicate) {
			sys.type = Type.Predicate;
		} else if (type == GlobalDefine.Type_CompositePredicate) {
			sys.type = Type.Predicate;
		} else if (type == GlobalDefine.Type_SimpleEA) {
			sys.type = Type.Protocol;
		} else if (type == GlobalDefine.Type_CompositeEA) {
			sys.type = Type.Protocol;
		} else if (type == GlobalDefine.Type_SimpleData) {
			sys.type = Type.Data;
		} else if (type == GlobalDefine.Type_CompositeData) {
			sys.type = Type.Data;
		} else if(type == GlobalDefine.Type_SIMPLE_SYSTEM
				|| type == GlobalDefine.Type_COMPOSITE_SYSTEM){
			sys.type = Type.System;
		}
	}
	
	private void convertCompositeModel(AbstractModel model, Sys sys) {
		List<AbstractModel> children = model.getChildren();
		sys.content.composite();
		for (AbstractModel child : children) {
			if (child instanceof DSLabel) {
				continue;
			}
			AbstractSysForViewModel sub = null;
			if (child instanceof com.dss.dsc.model.BeginPoint) {
				//dsc.model.BeginPoint beginPoint 
				sub = new BeginPoint();
				convertBeginPoint(sub, child, sys);
			} else if (child instanceof com.dss.dsc.model.EndPoint) {
				//EndPoint endPoint 
				sub = new EndPoint();
				convertEndPoint(sub, child, sys);
			} else if (child instanceof com.dss.dsc.model.ExitPoint) {
				//ExitPoint exitPoint 
				sub = new ExitPoint();
				convertExitPoint(sub, child, sys);
			} else if (child instanceof com.dss.dsc.model.EntryStatus
					|| child instanceof com.dss.dsc.model.ExitStatus) {
				//PreCondition preCond 
				sub = new PreCondition();
				convertConditon(sub, child, sys);
			} else {
				sub = new Sys();				
				convertAbstractModel(child, (Sys) sub);
				if (sys.content.composite().subSys == null) {
					sys.content.composite().subSys = new Array<Sys>();
				}
				sys.content.composite().subSys.inst.add((Sys) sub);
			}
			sub.superSys = new Location();
			sub.superSys.target = sys;
		}
	}
	
	private void convertEndPoint(AbstractSysForViewModel sub,AbstractModel child,Sys sys){
		child.sysObj = sub;
		((EndPoint)sub).id = ((com.dss.dsc.model.EndPoint) child).getEndValue();
		String name = child.getName();
		if(name.equals("")){
			name = namePrefix + child.getDiagramId();
		}
		sub.name = new StringBuffer(name);
		if (sys.content.composite().endPoint == null) {
			sys.content.composite().endPoint = new Array<EndPoint>();
		}
		sys.content.composite().endPoint.inst.add((EndPoint) sub);
		if(((EndPoint)sub).showInfo == null){
			((EndPoint)sub).showInfo = new Point();
		}
		((EndPoint)sub).showInfo.x = child.getConstraint().x;
		((EndPoint)sub).showInfo.y = child.getConstraint().y;
		bufConnection(((com.dss.dsc.model.EndPoint) child).getTargetConnection());
	}
	
	private void convertBeginPoint(AbstractSysForViewModel sub,AbstractModel child,Sys sys){
		child.sysObj = sub;
		String name = child.getName();
		if(name.equals("")){
			name = namePrefix + child.getDiagramId();
		}
		sub.name = new StringBuffer(name);
		
		if (sys.content.composite().beginPoint == null) {
			sys.content.composite().beginPoint = new Array<BeginPoint>();
		}
		if(((BeginPoint)sub).showInfo == null){
			((BeginPoint)sub).showInfo = new Point();
		}
		((BeginPoint)sub).showInfo.x = child.getConstraint().x;
		((BeginPoint)sub).showInfo.y = child.getConstraint().y;
		sys.content.composite().beginPoint.inst.add((BeginPoint) sub);
		bufConnection(((com.dss.dsc.model.BeginPoint) child).getTargetConnection());
	}
	
	private void convertConditon(AbstractSysForViewModel sub,AbstractModel child,Sys sys){
		child.sysObj = sub;
		String name = child.getName();
		if(name.equals("")){
			name = namePrefix + child.getDiagramId();
		}
		((PreCondition)sub).name = new StringBuffer(name);
		if (((com.dss.dsc.model.Status) child).getPredicat() != null) {
			((PreCondition) sub).condition = new StringBuffer(((com.dss.dsc.model.Status) child).getPredicat().getName());
		}
		if(child instanceof EntryStatus){
			if(sys.content.composite().preCondition == null){
				sys.content.composite().preCondition = new Array<PreCondition>();
				sys.content.composite().preCondition.inst.add((PreCondition) sub);
			}		
		}else if(child instanceof ExitStatus){
			if(sys.content.composite().postCondition == null){
				sys.content.composite().postCondition = new Array<PreCondition>();
				sys.content.composite().postCondition.inst.add((PreCondition) sub);
			}		
		}		
		((PreCondition)sub).showInfo = getAroundEdgePointShowInfo(child);	
		bufConnection(((com.dss.dsc.model.Status) child).getTargetConnection());
	}
		
	private void convertExitPoint(AbstractSysForViewModel sub,AbstractModel child,Sys sys){
		child.sysObj = sub;
		String name = child.getName();
		if(name.equals("")){
			name = namePrefix + child.getDiagramId();
		}
		sub.name = new StringBuffer(name);
		((ExitPoint)sub).exitCode = ((com.dss.dsc.model.ExitPoint) child).getExitValue();		
		
		ExitCondition exitCond =  ((com.dss.dsc.model.ExitPoint) child).getPredExitCondition();
		if (exitCond == ExitCondition.AndCondition){
			sub.name = new StringBuffer("AND");
		}else if (exitCond == ExitCondition.OrCondition){
			sub.name = new StringBuffer("OR");
		}else if (exitCond == ExitCondition.XorCondition){
			sub.name = new StringBuffer("XOR");
		}
		
		if (sys.content.composite().exitPoint == null) {
			sys.content.composite().exitPoint = new Array<ExitPoint>();
		}
		
		sys.content.composite().exitPoint.inst.add((ExitPoint) sub);
		
		if(((ExitPoint)sub).showInfo == null){
			((ExitPoint)sub).showInfo = getAroundEdgePointShowInfo(child);			
		}
		bufConnection(((com.dss.dsc.model.ExitPoint) child).getTargetConnection());
	}
	
	/**
	 * 适用于 exitpoint pre post condition的转换
	 * @param point
	 * @return
	 */
	private Point getAroundEdgePointShowInfo(AbstractModel point){
		Point showInfo = new Point();
		if(point.getConstraint().x + point.getConstraint().width + 1== point.getParent().getConstraint().width){
			showInfo.x = ToolMethod.translateToAbsolute(point).x + point.getConstraint().width + 1;
			showInfo.y = ToolMethod.translateToAbsolute(point).y;
		}else if(point.getConstraint().x  == 0){
			showInfo.x = ToolMethod.translateToAbsolute(point).x;
			showInfo.y = ToolMethod.translateToAbsolute(point).y;
		}else if(point.getConstraint().y + point.getConstraint().height + 1  == point.getParent().getConstraint().height){
			showInfo.x = ToolMethod.translateToAbsolute(point).x;
			showInfo.y = ToolMethod.translateToAbsolute(point).y + point.getConstraint().height + 1;
		}else if(point.getConstraint().y == 0 
				|| point.getConstraint().y == GlobalDefine.LABEL_HIGH){
			showInfo.x = ToolMethod.translateToAbsolute(point).x;
			showInfo.y = ToolMethod.translateToAbsolute(point).y - 1;
		}
		return showInfo;
	}
	
	private void convertSequentialBlock(SequentialBlock seqBlock, Sys sys) {
		if (seqBlock.getChildren().size() > 1) {
			AbstractModel sub = seqBlock.getChildren().get(1);
			if (sub instanceof FlowLayoutContainer) {
				List<AbstractModel> children = sub.getChildren();
				if (children.size() > 0) {
					sys.content.composite().subSys = new Array<Sys>();
				}
				for (AbstractModel child : children) {
					Sys subSys = new Sys();
					convertAbstractModel(child, subSys);
					sys.content.composite().subSys.inst.add(subSys);
				}
			}
		}
	}
	
	private void convertSpecSys(AbstractModel model, Sys sys) {
		int type = model.getDsType();
		if (model instanceof CompositeIA
				|| (model instanceof ExpressionElement && type == GlobalDefine.Type_SimpleIA)
				|| type == GlobalDefine.Type_SIMPLE_WORK || type == GlobalDefine.Type_SIMPLE_WORK_PROG 
				|| type == GlobalDefine.Type_SIMPLE_WORK_SCRIPT) {
			int deriveFlag = getDeriveDirection((Node) model);			
			if (deriveFlag == Node.RECEIVE) {
				sys.specSys = new SpecSys();
				sys.specSys.sWork().deriveInfo = new DeriveInfo();
				sys.specSys.sWork().deriveInfo.deriveFlag = DeriveFlag.receiver;
				if (((Node) model).getProtocolName() != null) {
					sys.specSys.sWork().deriveInfo.deriveProtoName = new StringBuffer(((Node) model).getProtocolName());
					Interaction interaction = getParitiInteraction(model, ((Node) model).getProtocolName());
					if (interaction != null && interaction.isVirtual()) {
						sys.specSys.sWork().deriveInfo.deriveProtoName = null;
					} else {
						String[] names = ((Node) model).getProtocolName().split("\\.");
						if (names.length > 0) {
							sys.specSys.sWork().deriveInfo.partiProtoName = new StringBuffer(names[0]);
						}
					}
				}
			} else if (deriveFlag == Node.SEND) {
				sys.specSys = new SpecSys();
				sys.specSys.sWork().deriveInfo = new DeriveInfo();
				sys.specSys.sWork().deriveInfo.deriveFlag = DeriveFlag.sender;
				if (((Node) model).getProtocolName() != null) {
					sys.specSys.sWork().deriveInfo.deriveProtoName = new StringBuffer(((Node) model).getProtocolName());
					Interaction interaction = getParitiInteraction(model, ((Node) model).getProtocolName());
					if (interaction != null && interaction.isVirtual()) {
						sys.specSys.sWork().deriveInfo.deriveProtoName = null;
					} else {
						String[] names = ((Node) model).getProtocolName().split("\\.");
						if (names.length > 0) {
							sys.specSys.sWork().deriveInfo.partiProtoName = new StringBuffer(names[0]);
						}
					}
				}
			}
		} else if (type == GlobalDefine.Type_SimpleEA) {
			sys.specSys = new SpecSys();
			sys.specSys.sProto().from = new StringBuffer(((SimpleEA) model).getSender());
			sys.specSys.sProto().to = new StringBuffer(((SimpleEA) model).getReceiver());
		}
	}
	
	private Interaction getParitiInteraction(AbstractModel model, String deriveProtoName) {
		AbstractModel superModel = model.getRealParent();
		String[] names = deriveProtoName.split("\\.");
		while (superModel instanceof Node) {
			for (AbstractConnectionModel conn : ((Node) superModel).getSourceConnection()) {
				if (conn instanceof Interaction) {
					String[] protoName = ((Interaction) conn).getName().split("\\.");
					if (protoName.length > 0 && names.length > 0
							&& protoName[0].equals(names[0])) {
						return (Interaction) conn;
					}
				}
			}
			
			for (AbstractConnectionModel conn : ((Node) superModel).getTargetConnection()) {
				if (conn instanceof Interaction) {
					String[] protoName = ((Interaction) conn).getName().split("\\.");
					if (protoName.length > 0 && names.length > 0
							&& protoName[0].equals(names[0])) {
						return (Interaction) conn;
					}
				}
			}
			
			superModel = superModel.getRealParent();
		}
		return null;
	}
	
	private void bufConnection(List<AbstractConnectionModel> connList) {
		for (AbstractConnectionModel conn : connList) {
			if (!connListBuf.contains(conn)) {
				Link link = new Link();
				conn.sysObj = link;
				connListBuf.add(conn);
				bufConnection(conn.getSourceConnection());
			}
		}
	}
	
	private void convertConnection(AbstractConnectionModel conn/*, Link link*/) {
		Link link = (Link) conn.sysObj;
		link.isVirtual = YesNo.no;
		link.isDerived = YesNo.no;
		if(link.showInfo == null){
			link.showInfo = new LinkShowInfo();
		}
		link.showInfo.fromAngle = conn.getSourceAnchorAngle();
		link.showInfo.toAngle = conn.getTargetAnchorAngle();
		
		if(conn.isVisible()){
			link.showInfo.visible = YesNo.yes;
		}else{
			link.showInfo.visible = YesNo.no;
		}
		
		if (conn instanceof Transition) {
			link.name = new StringBuffer("transition"+conn.diagramId);
			link.linkType = LinkType.transition;
			link.content = new StringBuffer(((Transition) conn).getCondition());
		} else if (conn instanceof Interaction) {
			link.name = new StringBuffer("interaction"+conn.diagramId);
			link.linkType = LinkType.interaction;
			if (((Interaction) conn).isVirtual()) {
				link.isVirtual = YesNo.yes;
			}
			link.content = new StringBuffer(conn.getName());
		} else if (conn instanceof DynamicCreation) {
			link.name = new StringBuffer("create"+conn.diagramId);
			link.linkType = LinkType.create;
			if (conn.getDeriveId() > 0) {
				link.isDerived = YesNo.yes;
			}
			link.content = new StringBuffer(conn.getName());
		} else if (conn instanceof Specialization) {
			link.name = new StringBuffer("inherit"+conn.diagramId);
			link.linkType = LinkType.inherit;
		}
		
		Node source = conn.getSource();
		Node target = conn.getTarget();
		
		// 这里的getRefTarget会用到ProjectInfo，但是ProjectInfo不是保存在lib中的ProjectInfo class中，
		// 而是保存在plugin中ProjectInfo class中，因而，下面可能找不到target。
//		if (source instanceof NameLabel) {
//			AbstractModel model = GlobalDSNameCache.getRefTarget(source.getName(), source);
//			if (model instanceof Node) {
//				source = (Node) model;
//			}
//		}
//		if (target instanceof NameLabel) {
//			AbstractModel model = GlobalDSNameCache.getRefTarget(target.getName(), source);
//			if (model instanceof Node) {
//				target = (Node) model;
//			}
//		}
		
		link.from = new Location();
		link.to = new Location();
		link.from.target = source.sysObj;
		link.to.target = target.sysObj;
		if (link.from.target instanceof Sys) {
			if (((Sys) link.from.target).linkFrom == null) {
				((Sys) link.from.target).linkFrom = new Array<Link>();
			}
			((Sys) link.from.target).linkFrom.inst.add(link);
		} else if (link.from.target instanceof dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint) {
			if (((dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint) link.from.target).transFrom == null) {
				((dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint) link.from.target).transFrom = new Array<Link>();
			}
			((dsc.model.view.SysForView.Complete.Sys.Content.Composite.BeginPoint) link.from.target).transFrom.inst.add(link);
		} else if (link.from.target instanceof dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) {
			if (((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.from.target).transFrom == null) {
				((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.from.target).transFrom = new Array<Link>();
			}
			((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.from.target).transFrom.inst.add(link);
		} else if (link.from.target instanceof Link) {
			if (((Link) link.from.target).linkFrom == null) {
				((Link) link.from.target).linkFrom = new Array<Link>();
			}
			((Link) link.from.target).linkFrom.inst.add(link);
		} 
		
		if (link.to.target instanceof Sys) {
			if (((Sys) link.to.target).linkTo == null) {
				((Sys) link.to.target).linkTo = new Array<Link>();
			}
			((Sys) link.to.target).linkTo.inst.add(link);
		} else if (link.to.target instanceof dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) {
			if (((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.to.target).transTo == null) {
				((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.to.target).transTo = new Array<Link>();
			}
			((dsc.model.view.SysForView.Complete.Sys.Content.Composite.ExitPoint) link.to.target).transTo.inst.add(link);
		}  else if (link.to.target instanceof dsc.model.view.SysForView.Complete.Sys.Content.Composite.EndPoint) {
			if (((dsc.model.view.SysForView.Complete.Sys.Content.Composite.EndPoint) link.to.target).transTo == null) {
				((dsc.model.view.SysForView.Complete.Sys.Content.Composite.EndPoint) link.to.target).transTo = new Array<Link>();
			}
			((dsc.model.view.SysForView.Complete.Sys.Content.Composite.EndPoint) link.to.target).transTo.inst.add(link);
		}
	}
	
	private void convertConnection(Array<Link> linkList) {
//		for (AbstractConnectionModel conn : connListBuf) {
//			Link link = new Link();
//			convertConnection(conn, link);
//			linkList.inst.add(link);
//		}
		if (linkList.instanceMap == null) {
			linkList.instanceMap = new java.util.HashMap<String, Link>();
		}
		for (AbstractConnectionModel conn : connListBuf) {
//			Link link = new Link();
			convertConnection(conn);
			Link link = (Link) conn.sysObj;
			if (link != null && link.name != null && link.name.length() > 0) {
				linkList.instanceMap.put(link.name.toString(), link);
			}
			linkList.inst.add((Link) conn.sysObj);
		}
	}
	
	private int getDeriveDirection(Node model) {
		if (model instanceof ExpressionElement) {
			if (((ExpressionElement) model).isReceive()) {
				return Node.RECEIVE;
			} else if (((ExpressionElement) model).isSend()) {
				return Node.SEND;
			}
		} else if (model instanceof CompositeIA) {
			if (((CompositeIA) model).isReceive()) {
				return Node.RECEIVE;
			} else if (((CompositeIA) model).isSend()) {
				return Node.SEND;
			}
		} else if (model instanceof CompositeSystem) {
			if (((CompositeSystem) model).isReceive()) {
				return Node.RECEIVE;
			} else if (((CompositeSystem) model).isSend()) {
				return Node.SEND;
			}
		} else if (model instanceof SimpleSystem) {
			if (((SimpleSystem) model).isReceive()) {
				return Node.RECEIVE;
			} else if (((SimpleSystem) model).isSend()) {
				return Node.SEND;
			}
		}
		return 0;
	}
	
	public static void main(String[] args) {
		GefModelUtil mUtil = GefModelUtil.instance();
		Object obj = mUtil.readFile("C:\\Documents and Settings\\Administrator\\桌面\\webEditor\\eclipse\\GUIarch.dsc");
		Sys sys = new Sys();
		Array<Link> links= new Array<Link>();
		mUtil.convertContentsModel((ContentsModel) obj,sys,links);
		Complete complete = new Complete();
		complete.link = links;
//		complete.sys = sys.content.composite().subSys;
		SysForView sysForView = new SysForView();
		sysForView.pick = SysForView.complete_id;
		sysForView.picked = complete;
		
		String s = JsonConvert.instance().sysForView2Json(sysForView);
		System.out.println(s);
		
		
	}
}